<?php

namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\Module;
use cms\modules\v1\models\access\ModuleActions;
use common\models\Functions;
use common\models\Status;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

/**
 * Default controller for the `v1` module
 */
class AccessController extends DefaultController
{

    public $modelClass = 'cms\modules\v1\models\access\Module';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [
                ]
            ]
        ]);
    }
    /* ======================  模块设置 ========================= */
    public function actionMenu() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new Module())->getModules();
        }
    }

    public function actionModule() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new Module())->getTree();
        }
    }

    public function actionModuleMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new Module())->getTreeMap();
        }
    }

    public function actionModuleInfo($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new Module())->getInfo($id);
        }

        if (Yii::$app->request->isPost) {
            return (new Module())->saveInfo();
        }
    }

    public function actionModuleOrder() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isPost) {
            return (new Module())->saveOrder();
        }
    }
    public function actionModuleStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isPost) {
            return (new Module())->switchStatus();
        }
    }

    /* ======================  模块方法设置 ========================= */

    public function actionActionRequestMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0,'',Status::requestTypeMap());
        }
    }

    public function actionModuleActionAddEdit($moduleId = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        $model = new ModuleActions();
        if (Yii::$app->request->isGet) {
            return $model->getInfo($moduleId);
        }

        if (Yii::$app->request->isPost) {
            return $model->addEdit();
        }
    }
}
